<?php
class db_permanent_link extends db_table
{
	public $table_name = 'permanent_link' ;
	
	function __construct()
	{
		return parent::__contruct() ;
	}
	function read($permanent_link=''){
		$sql = " SELECT * FROM {prefix}permanent_link WHERE permanent_link = '{$permanent_link}' LIMIT 1 ; " ;
		return $this->query($sql) ;
	}
	function exists($permanent_link='' ,$content_id = 0 ,$module_id = 0){
		$sql = " SELECT * FROM {prefix}permanent_link " ;
		$sql .= " WHERE permanent_link = '{$permanent_link}' " ;
		if($content_id && $module_id)
		{
			$sql .= " AND content_id <> {$content_id} AND module_id <> {$module_id}  " ;
		}
		$sql .= " LIMIT 1 ; " ;
		return $this->query($sql) ;
	}
	function update($permanent_link = '' ,$content_id = 0 ,$module_id = 0)
	{
		$result = 0 ;
		if($permanent_link && $content_id && $module_id)
		{
			$exists = $this->exists($permanent_link='' ,$content_id = 0 ,$module_id = 0) ;
			if(!$exists)
			{
				$sql = " UPDATE {prefix}permanent_link " ;
				$sql .= " SET permanent_link = '{$permanent_link}' " ;
				$sql .= " WHERE content_id = '{$content_id}' AND module_id = '{$module_id}' LIMIT 1 ; " ;
				$result =  $this->insert($sql) ;
			}
		}
		return $result ;
	}
	function insert($permanent_link = '' ,$content_id = 0 ,$module_id = 0)
	{
		$result = 0 ;
		if($permanent_link && $content_id && $module_id)
		{
			$exists = $this->exists($permanent_link='' ,$content_id = 0 ,$module_id = 0) ;
			if(!$exists)
			{
				$sql = " INSERT INTO {prefix}permanent_link " ;
				$sql .= " SET permanent_link = '{$permanent_link}' ," ;
				$sql .= " content_id = '{$content_id}' , module_id = '{$module_id}' ; " ;
				$result =  $this->insert($sql) ;
			}
		}
		return $result ;
	}
}